package com.boxin.ims.modules.wechat.busi;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.ims.modules.wechat.entity.LotteryConfig;
import com.boxin.ims.modules.wechat.entity.LotteryScope;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.LotteryConfigService;
import com.boxin.ims.modules.wechat.service.LotteryScopeService;
import com.boxin.ims.modules.wechat.service.WechatDaoHelper;
import com.thinkgem.jeesite.common.utils.DateUtils;

/**
 * @author Jakemanse
 * @date 2013-10-23
 * @function <p> 现场抽奖加入队列 </p>
 */
@Service
public class LotteryJoinProcessor extends BusinessProcessor{
	
	@Autowired
	LotteryConfigService lotteryConfigService;
	
	@Autowired
	LotteryScopeService lotteryScopeService;
	
	@Autowired
	WechatDaoHelper wechatDaoHelper;

	public LotteryJoinProcessor() {
		super();
	}
	
	
	@Override
	public ProcessMessage process() {
		
		WechatMember member =	memberCheckAndReply();
		if(member!=null){
			
			String reply = "";
			LotteryConfig config = lotteryConfigService.getByUserId(user.getId());
			if(config !=null && !StringUtils.isEmpty(config.getQueueKeywords())){
				if(StringUtils.isEmpty(reply)){
					reply = "您已成功登记到抽奖队列中，敬请等待抽奖的开始...";
				}
				reply = "您已成功登记到抽奖队列中，敬请等待抽奖的开始...";
				
				//判断是否已经加入排队
				if(!wechatDaoHelper.isInLotteryQueue(member)){//没有在队列中，则加入
					LotteryScope lotteryScope = new LotteryScope();
					lotteryScope.setDay(DateUtils.getTodayString());
					lotteryScope.setMember(member);
					lotteryScope.setUser(user);
					lotteryScope.setMobile(member.getMobile());
					
					lotteryScopeService.save(lotteryScope);
				}else{//已经加入过队列
					reply = "亲，您已经加入队列了,敬请等候抽奖的开始...";
				}		
			}else{
				reply = "现场抽奖活动还没有启动，谢谢您的参与!";
			}
			
			//返回给用户信息
			writeToUser(reply);
		}
		return null;
	}

	@Override
	public boolean isNeedBeMember() {
		
		return false;
	}

}
